﻿using Business.Service;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcAppWeb.Controllers
{
    public class DCWarehouseController : BaseDataController
    {

        public ActionResult DCWarehouse()
        {
            if (Session["UserInfo"] == null) return RedirectToAction("LogOn", "Account");
            else
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                return View(model);

            }
        }


        ///<summary>
        ///查询前置仓仓库信息
        ///</summary>
        ///<returns></returns>
        public JsonResult InitJsonResult(string warehouseCode, string status)
        {
            string where = "  where 1=1 ";
            int page = int.Parse(Request["page"].ToString());
            int rows = int.Parse(Request["rows"].ToString());
            int total = 0;
            if (warehouseCode != "")
            {
                where += " and warehouseCode='" + warehouseCode + "'";
            }
            if (status != "")
            {
                where += " and status='" + status + "'";
            }
            var retData = new object();
            BDCwarehouse db = new BDCwarehouse();
            retData = db.GetDCwarehouseInfo(page, rows, out total, where).ToList();
            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", total);
            json.Add("rows", retData);
            return Json(json);
        }


        /// <summary>
        /// 获取仓库信息
        /// </summary>
        /// <returns></returns>
        public JsonResult GetDCWarehouseList(string defaultValue)
        {
            string defaultValue1 = Request["defaultValue"].ToString();
            var retData = new object();
            BDCwarehouse db = new BDCwarehouse();
            retData = db.GetDCWarehouseList(defaultValue).ToList();
            return Json(retData);
        }


        /// <summary>
        /// 修改or新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string SaveData()
        {
            string sjson = string.Empty;
            try
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                DCwarehouse dw = new DCwarehouse();

                string warehouseId = Request["hiddenWAREHOUSEID"]; ;

                dw.WAREHOUSECODE = Request["stxtWarehouseCode"].ToString();
                dw.WAREHOUSENAME = Request["stxtWarehouseName"].ToString();
                dw.WAREHOUSEPROVINCE = Request["province"].ToString();
                dw.WAREHOUSECITY = Request["city"].ToString();
                dw.WAREHOUSEDISTRICT = Request["district"].ToString();
                dw.WAREHOUSEADDRESS = Request["txtADDRESS"].ToString();
                dw.OPRNO = model.USERCODE.ToString();
                dw.STATUS = Request["sstatus"].ToString();

                BDCwarehouse db = new BDCwarehouse();

                if (string.IsNullOrEmpty(warehouseId))
                {
                    List<DCwarehouse> list = db.GetDCwarehouseByCode(dw.WAREHOUSECODE);
                    if (list.Count > 0)
                    {
                        sjson = "{success:false,msg:'此仓库编号已经存在！'}";
                    }
                    else
                    {
                        int n = db.AddDCwarehouse(dw);

                        if (n > 0)
                        {
                            sjson = "{success:true}";
                        }
                        else
                        {
                            sjson = "{success:false,msg:'添加失败.请联系管理员！'}";
                        }
                    }
                }
                else
                {
                    dw.WAREHOUSEID = int.Parse(warehouseId);
               
                    int n = db.UpdateDCwarehouse(dw);
                    if (n > 0)
                    {
                        sjson = "{success:true}";
                    }
                    else
                    {
                        sjson = "{success:false,msg:'修改失败.请联系管理员！'}";
                    }
                }


                return sjson;

            }
            catch (Exception ex)
            {
                sjson = "{success:false,msg:'" + ex.Message + "'}";
            }
            return sjson;
        }

    }
}
